package com.example.cloud.controller.statistics;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.cloud.controller.common.BaseController;
import com.example.cloud.model.dto.FilterModel;
import com.example.cloud.model.dto.QueryModel;
import com.example.cloud.model.dto.Result;
import com.example.cloud.model.statistics.StatisticsFinancial;
import com.example.cloud.service.statistics.StatisticsFinancialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by wt on 2018/10/25.
 */
@RestController
@RequestMapping("/statistics/financial")
public class StatisticsFinancialController extends BaseController {

    @Autowired
    private StatisticsFinancialService statisticsFinancialService;

    @PostMapping("/search")
    public Result search(@RequestBody QueryModel model) {
        Result result = this.singleQuery(model, StatisticsFinancial.class);
        boolean c = false;
        String t = "";
        Map<String, String> p = new HashMap();
        for (FilterModel f : model.getDefaultFilters()) {
            if(p.containsKey(f.getField())){
                String v1 = p.get(f.getField());
                String v2 = f.getValue1();
                v2 = v2.substring(0, v2.indexOf(" "));
                if(!v1.equals(v2)){
                    c = true;
                    break;
                }
            }else{
                String v = f.getValue1();
                v = v.substring(0, v.indexOf(" "));
                p.put(f.getField(), v);
            }
        }
        // 查询跨天需要合并处理
        if(c){
            JSONArray jsonArray = (JSONArray) result.getData();
            List<StatisticsFinancial> list = new ArrayList<>();
            for (Object o : jsonArray) {
                list.add(JSON.parseObject(JSON.toJSONString(o), StatisticsFinancial.class));
            }
            Map<Long, List<StatisticsFinancial>> mapList = list.stream().collect(Collectors.groupingBy(StatisticsFinancial::getUserId));
            list = new ArrayList<>();
            for (Map.Entry<Long, List<StatisticsFinancial>> entry : mapList.entrySet()) {
                int newNumber = 0;
                int newCourseTransaction = 0;
                int newSingleTransaction = 0;
                int reviewSingleTransaction = 0;
                int treatTransaction = 0;
                int reviewRenewalCourseTransaction = 0;
                BigDecimal newCourseAmount = BigDecimal.ZERO;
                BigDecimal newSingleAmount = BigDecimal.ZERO;
                BigDecimal newPayTotalAmount = BigDecimal.ZERO;
                BigDecimal reviewSingleReamount = BigDecimal.ZERO;
                BigDecimal reviewRenewalCourseAmount = BigDecimal.ZERO;
                BigDecimal deductAmount = BigDecimal.ZERO;
                for (StatisticsFinancial v : entry.getValue()) {
                    newNumber += v.getNewNumber();
                    newCourseTransaction += v.getNewCourseTransaction();
                    newSingleTransaction += v.getNewSingleTransaction();
                    reviewSingleTransaction += v.getReviewSingleTransaction();
                    reviewRenewalCourseTransaction += v.getReviewRenewalCourseTransaction();
                    treatTransaction += v.getTreatTransaction();
                    if(v.getNewCourseAmount() != null){
                        newCourseAmount = newCourseAmount.add(v.getNewCourseAmount());
                    }
                    if(v.getNewSingleAmount() != null){
                        newSingleAmount = newSingleAmount.add(v.getNewSingleAmount());
                    }
                    if(v.getNewPayTotalAmount() != null){
                        newPayTotalAmount = newPayTotalAmount.add(v.getNewPayTotalAmount());
                    }
                    if(v.getReviewSingleReamount() != null){
                        reviewSingleReamount = reviewSingleReamount.add(v.getReviewSingleReamount());
                    }
                    if(v.getReviewRenewalCourseAmount() != null){
                        reviewRenewalCourseAmount = reviewRenewalCourseAmount.add(v.getReviewRenewalCourseAmount());
                    }
                    if(v.getDeductAmount() != null){
                        deductAmount = deductAmount.add(v.getDeductAmount());
                    }
                }
                StatisticsFinancial s = new StatisticsFinancial();
                s.setUserId(entry.getKey());
                s.setUserName(entry.getValue().get(0).getUserName());
                s.setNewNumber(newNumber);
                s.setNewCourseTransaction(newCourseTransaction);
                s.setNewCourseAmount(newCourseAmount);
                s.setNewSingleTransaction(newSingleTransaction);
                s.setNewSingleAmount(newSingleAmount);
                s.setNewPayTotalAmount(newPayTotalAmount);
                s.setReviewSingleTransaction(reviewSingleTransaction);
                s.setReviewSingleReamount(reviewSingleReamount);
                s.setReviewRenewalCourseTransaction(reviewRenewalCourseTransaction);
                s.setReviewRenewalCourseAmount(reviewRenewalCourseAmount);
                s.setTreatTransaction(treatTransaction);
                s.setDeductAmount(deductAmount);
                list.add(s);
            }
            result.setData(JSONArray.parseArray(JSON.toJSONString(list)));
        }
        return result;
    }

    @PostMapping("/saveOrUpdate")
    public void saveOrUpdate(@RequestBody StatisticsFinancial record){
        statisticsFinancialService.doSave(record);
    }
}
